using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace System.IPod.Music
{
	// Song description
	[StructLayout(LayoutKind.Sequential, Pack = 1)]
	unsafe struct Mhit
	{
		public int TotalLength;
		public int StringCount;
		public int Id;
		public int Visible;
		public int FileType;
		public short Type;
		public byte CompilationFlag;
		public byte Stars;
		public int DateAdded;
		public int Size;
		public int Length;
		public int TrackNumber;
		public int TotalTracks;
		public int Year;
		public int Bitrate;
		public short Unknown;
		public short Samplerate;
		public int Volume;
		public int StartTime;
		public int StopTime;
		public int SoundCheck;
		public int PlayCount;
		public int PlayCount2;
		public int LastPlayedTime;
		public int DiscNumber;
		public int TotalDiscs;
		public int UserId;
		public int LastModifiedTime;
		public int BookmarkTime;
		public long Dbid;
		public byte Checked;
		public byte ApplicationRating;
		public short Bmp;
		public short ArtworkCount;
		public short Unknown9;
		public int ArtworkSize;
		public int Unknown11;
		public int SampleRate2;
		public int DateReleased;
		public int Unknown14;
		public int Unknown15;
		public int Unknown16;
		public int Unknown17;
		public int Unknown18;
		public byte Flag1;
		public byte Flag2;
		public byte Flag3;
		public byte Flag4;
		public long Dbid2;
		public byte Unknown19;
		public byte MovieFileFlag;
		public short Unknown20;
		public int Unknown21;
		public int Unknown22;
		public int SampleCount;
		public int Unknown24;
		public int Unknown25;
		public int Unknown26;
		public int Unknown27;
		public int MediaType;
		public int SeasonNumber;
		public int EpisodeNumber;
		public int Unknown31;
		public int Unknown32;
		public int Unknown33;
		public int Unknown34;
		public int Unknown35;
		public int Unknown36;
		public int Unknown37;
		public int GaplessData;
		public int Unknown38;
		public short GaplessTrackFlag;
		public short GaplessAlbumFlag; // size = 260
		
		public static Mhit Create()
		{
			Mhit mhit = new Mhit();
			//mhit.Identifier[0] = (byte)'m';
			//mhit.Identifier[1] = (byte)'h';
			//mhit.Identifier[2] = (byte)'i';
			//mhit.Identifier[3] = (byte)'t';
			
			//mhit.HeaderLength = 328;
			//mhit.FileType = 0x4d503320;
			
			return mhit;
		}

		public void Read(BinaryReader reader)
		{
			MhHeader header = new MhHeader();
			header.Read(reader);
			Debug.Assert(header.Identifier[0] == 0x6D &&
				header.Identifier[1] == 0x68 &&
				header.Identifier[2] == 0x69 &&
				header.Identifier[3] == 0x74,
				"Mhit missmatch");
			
			byte[] data = reader.ReadBytes(header.Length - sizeof(MhHeader));
			fixed (byte* pData = data)
			{
				this = *(Mhit*)pData;
			}

			// Padded zeros
			//reader.ReadBytes(HeaderLength - sizeof(Mhit));
		}

		public void Write(BinaryWriter writer)
		{
			//byte[] data = StructUtil.GetBytes(this);
			//writer.Write(data);

			//int count = HeaderLength - sizeof(Mhit);
			//for (int i = 0; i < count; i++)
			//    writer.Write((byte)0);
		}
	}
}
